class PropertyPatternPropertyAttribute < ActiveRecord::Base
  acts_as_nested_set :dependent => :destroy, :scope => :property_pattern_property_id

  belongs_to :property, :touch => true, :class_name => "PropertyPatternProperty", :foreign_key => "property_pattern_property_id"

  has_one  :property_value, :class_name => "PropertyPatternPropertyValue", :foreign_key => "property_pattern_property_attribute_id"

  has_one  :property_enumerable,  :as => :enumerable
  has_one  :property_enum_group,  :through => :property_enumerable
  has_many :property_enum_values, :through => :property_enum_group
  
  validates :presentation, :presence => true, :length => {:maximum => 35}

  attr_accessor :node_parent_id, :node_next_id, :node_prev_id, :node_id

  accepts_nested_attributes_for :property_enum_group, :allow_destroy => true
  accepts_nested_attributes_for :property_enumerable, :allow_destroy => true
  accepts_nested_attributes_for :property_value, :allow_destroy => true, :reject_if => proc {|attr| attr['value'].blank?}

  alias new_property_enumerable property_enumerable
  def property_enumerable
    new_property_enumerable || build_property_enumerable
  end

  alias new_property_enum_group property_enum_group
  def property_enum_group
    new_property_enum_group || property_enumerable.build_property_enum_group
  end

  alias new_property_value property_value
  def property_value
    new_property_value || build_property_value
  end
end